{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python算术运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "c =  30\n",
      "1 - c =  -29\n",
      "a / b =  0.5\n",
      "b / a =  2.0\n",
      "a % b =  10\n",
      "b % a =  0\n"
     ]
    }
   ],
   "source": [
    "# Python2.x 里，整数除整数，只能得出整数。如果要得到小数部分，把其中一个数改成浮点数即可。\n",
    "a = 10\n",
    "b = 20\n",
    "c = 0\n",
    "c = a + b\n",
    "print(\"c = \",c)\n",
    "print(\"1 - c = \",1 - c)\n",
    "print('a / b = ',a / b)\n",
    "print('b / a = ',b / a)\n",
    "print('a % b = ', a % b)\n",
    "print('b % a = ', b % a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python比较运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a == b  False\n",
      "a != b  True\n",
      "a > b  True\n",
      "b < a  True\n",
      "a >= b  True\n"
     ]
    }
   ],
   "source": [
    "a = 21\n",
    "b = 10\n",
    "\n",
    "print(\"a == b \",a == b)\n",
    "print(\"a != b \",a != b)\n",
    "print(\"a > b \",a > b)\n",
    "print(\"b < a \",b < a)\n",
    "print(\"a >= b \",a >= b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python赋值运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a + b 30\n",
      "b - a  10\n",
      "c += a  20\n",
      "c *= a  200\n",
      "c **= a  102400000000000000000000\n"
     ]
    }
   ],
   "source": [
    "a = 10\n",
    "b = 20\n",
    "c = a+b\n",
    "print('a + b',c)\n",
    "c = b - a\n",
    "print('b - a ',c)\n",
    "c += a\n",
    "print('c += a ',c)\n",
    "c *= a\n",
    "print('c *= a ',c)\n",
    "c **= a\n",
    "print('c **= a ',c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['1223']\n",
      "['hhhh', 'aaaaa']\n"
     ]
    }
   ],
   "source": [
    "list = ['1223','hhhh','aaaaa']\n",
    "print(list[:1])\n",
    "print(list[1:])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python位运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a&b =  12\n",
      "a|b =  61\n",
      "a^b =  49\n",
      "~a =  -61\n",
      "a << 2 =  240\n",
      "a >> 2 =  15\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "按位运算符是把数字看作二进制来进行计算的。Python中的按位运算法则如下：\n",
    "下表中变量 a 为 60，b 为 13，二进制格式如下：\n",
    "'''\n",
    "a = 60 # 0011 1100\n",
    "b = 13 # 0000 1101\n",
    "print(\"a&b = \",a&b) # 按位与运算符：参与运算的两个值,如果两个相应位都为1,则该位的结果为1,否则为0\n",
    "print(\"a|b = \",a|b) # 按位或运算符：只要对应的二个二进位有一个为1时，结果位就为1。\n",
    "print(\"a^b = \",a^b) # 按位异或运算符：当两对应的二进位相异时，结果为1\n",
    "print(\"~a = \",~a)# 按位取反运算符：对数据的每个二进制位取反,即把1变为0,把0变为1 。~x 类似于 -x-1\n",
    "print(\"a << 2 = \",a << 2)# 左移动运算符：运算数的各二进位全部左移若干位，由 << 右边的数字指定了移动的位数，高位丢弃，低位补0。\n",
    "print(\"a >> 2 = \",a >> 2 )# 右移动运算符：把\">>\"左边的运算数的各二进位全部右移若干位，>> 右边的数字指定了移动的位数\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python逻辑运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a and b =  20\n",
      "1 -  c 和 a 有一个不为 true\n",
      "1 -  c 和 d 有一个不为 true\n",
      "a or b =  10\n",
      "a = 0情况下 a or b =  20\n",
      "4 - 变量 a 和 b 都为 true，或其中一个变量为 true\n",
      "5 - 变量 a 和 b 都为 false，或其中一个变量为 false\n"
     ]
    }
   ],
   "source": [
    "\n",
    "#  https://blog.csdn.net/niuniuyuh/article/details/71213887  \n",
    "\n",
    "a = 10\n",
    "\n",
    "b = 20\n",
    "c = 0\n",
    "d = []\n",
    "\n",
    "if a and b :\n",
    "    print('a and b = ',(a and b))# x and y 布尔\"与\" - 如果 x 为 False，x and y 返回 False，否则它返回 y 的计算值。\n",
    "else:\n",
    "     print('1 -  a 和 b 有一个不为 true')\n",
    "if c and a :\n",
    "     print('c and a = ',(c and a))\n",
    "else:\n",
    "     print('1 -  c 和 a 有一个不为 true')\n",
    "        \n",
    "if c and d:\n",
    "     print('c and a = ',(c and d))\n",
    "else:\n",
    "     print('1 -  c 和 d 有一个不为 true')\n",
    "        \n",
    "if a or b :\n",
    "     print('a or b = ',(a or b))# 布尔\"或\"\t- 如果 x 是非 0，它返回 x 的值，否则它返回 y 的计算值。\n",
    "else:\n",
    "     print('2 - 变量 a 和 b 都不为 true')\n",
    "        \n",
    "# 修改变量 a 的值\n",
    "a = 0\n",
    "if  a and b :\n",
    "    print('a or b = ',(a or b))# 布尔\"或\"\t- 如果 x 是非 0，它返回 x 的值，否则它返回 y 的计算值。\n",
    "else:\n",
    "   print('a = 0情况下 a or b = ',(a or b))\n",
    "\n",
    "if a or b :\n",
    "   print (\"4 - 变量 a 和 b 都为 true，或其中一个变量为 true\")\n",
    "else:\n",
    "   print (\"4 - 变量 a 和 b 都不为 true\")\n",
    " \n",
    "if not( a and b ):\n",
    "   print (\"5 - 变量 a 和 b 都为 false，或其中一个变量为 false\") # 布尔\"非\" - 如果 x 为 True，返回 False 。如果 x 为 False，它返回 True。\n",
    "else:\n",
    "   print (\"5 - 变量 a 和 b 都为 true\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python成员运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      " a in list :  True\n",
      "b not in list  False\n"
     ]
    }
   ],
   "source": [
    "# in\t如果在指定的序列中找到值返回 True，否则返回 False。\n",
    "a = 50\n",
    "\n",
    "b = 70\n",
    "list = [20,50,60,70]\n",
    "print(' a in list : ', a in list)\n",
    "print('b not in list ',b not in list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python身份运算符"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a is b True\n",
      "修改 b值后  a is b False\n",
      "修改 b值后  a is not b True\n"
     ]
    }
   ],
   "source": [
    "# 身份运算符用于比较两个对象的存储单元\n",
    "# is 是判断两个标识符是不是引用自一个对象\n",
    "a = 20\n",
    "b = 20\n",
    "print('a is b', a is b)\n",
    "\n",
    "b = 30\n",
    "print('修改 b值后  a is b', a is b)\n",
    "\n",
    "\n",
    "#  is not 是判断两个标识符是不是引用自不同对象\n",
    "\n",
    "print('修改 b值后  a is not b', a is not b)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Python运算符优先级"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(a + b) * c / d 运算结果为： 90.0\n",
      "((a + b) * c) / d 运算结果为： 90.0\n",
      "(a + b) * (c / d) 运算结果为： 90.0\n",
      "a + (b * c) / d 运算结果为： 50.0\n"
     ]
    }
   ],
   "source": [
    "a = 20\n",
    "b = 10\n",
    "c = 15\n",
    "d = 5\n",
    "e = 0\n",
    "\n",
    "e = (a + b) * c / d       #( 30 * 15 ) / 5\n",
    "print (\"(a + b) * c / d 运算结果为：\",  e)\n",
    " \n",
    "e = ((a + b) * c) / d     # (30 * 15 ) / 5\n",
    "print (\"((a + b) * c) / d 运算结果为：\",  e)\n",
    " \n",
    "e = (a + b) * (c / d);    # (30) * (15/5)\n",
    "print (\"(a + b) * (c / d) 运算结果为：\",  e)\n",
    " \n",
    "e = a + (b * c) / d;      #  20 + (150/5)\n",
    "print (\"a + (b * c) / d 运算结果为：\",  e)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b == a :  True\n",
      "b is a :  True\n",
      "\n",
      " 上面 b = a  ================================================================================ 下面 b = a[:] \n",
      "\n",
      "b == a :  True\n",
      "b is a :  False\n"
     ]
    }
   ],
   "source": [
    "'''\n",
    "\n",
    "is 与 == 区别：\n",
    "\n",
    "is 用于判断两个变量引用对象是否为同一个(同一块内存空间)， == 用于判断引用变量的值是否相等。\n",
    "\n",
    "\n",
    "'''\n",
    "\n",
    "\n",
    "a = [1,2,3,4,5]\n",
    "\n",
    "b = a\n",
    "\n",
    "print('b == a : ',b == a)# True\n",
    "print('b is a : ',b is a)# True\n",
    "\n",
    "print('\\n','上面 b = a ','================================================================================','下面 b = a[:]','\\n')\n",
    "\n",
    "b = a[:]\n",
    "\n",
    "print('b == a : ',b == a)# True\n",
    "print('b is a : ',b is a)# False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
